<?php

namespace App\Models;

use App\Common\Constant\Params;
use Illuminate\Database\Eloquent\Model;

class Coupon extends BaseModel
{
    protected $table = 'coupon';

    protected $primaryKey = 'c_id';

    const CREATED_AT = 'c_add_time';
    const UPDATED_AT = 'c_upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'c_name',
        'c_vaildday',
        'c_vaildtype',
        'c_starttime',
        'c_endtime',
        'c_fullmoney',
        'c_money',
        'c_count',
        'c_stoptime',
        'c_grantuser'
    ];

    protected $casts = [
        'c_starttime'=>'datetime',
        'c_endtime'=>'datetime',
        'c_stoptime'=>'datetime',
        'c_fullmoney'=>'double',
        'c_money'=>'double',
        'c_count'=>'integer'
    ];

    /**
     * 过期方式文字
     * vaildtype_text
     */
    public function getVaildtypeTextAttribute()
    {
        return Params::$COUPON_VAILDTYPE[$this->c_vaildtype];
    }

    /**
     * 过期时间文字
     * vaildtime_text
     */
    public function getVaildtimeTextAttribute()
    {
        return $this->c_vaildtype == Params::COUPON_VAILDTYPE_DAYS
            ? $this->c_vaildday.'天'
            : $this->c_starttime->format('Y/m/d').'-'.$this->c_endtime->format('Y/m/d');
    }
}
